﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using com.pro2e.console.service.msg.vo;
using com.pro2e.web.util;
using com.pro2e.console.service.auth.vo;
using NHibernate;
using com.pro2e.console.core.hibernate;
using com.pro2e.console.service.log.vo;

/// <summary>
/// WebLogService 的摘要描述
/// </summary>
/// 

namespace com.pro2e.web.log
{
    public class WebLogService
    {
        log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        HttpHelper httpHelper = new HttpHelper();
        public WebLogService()
        {

        }

        /// <summary>
        /// 傳回whomak
        /// </summary>
        /// <returns></returns>
        public static string GetWhoMake()
        {
            SessionHelper shelper = new SessionHelper();

            LoginUser user = shelper.LoginUser;

            if (user == null)
            {
                return "";
            }
            else
            {
                if (user.UserId.Length > 10)
                {
                    return user.UserId.Substring(0, 10);
                }
                else
                {
                    return user.UserId;
                }
            }
        }

        /// <summary>
        /// 檢查密碼是否正確
        /// </summary>
        /// <param name="pw"></param>
        /// <returns></returns>
        public static bool CheckPassoword(string pw)
        {
            SessionHelper shelper = new SessionHelper();

            LoginUser user = shelper.LoginUser;

            if (user == null)
            {
                return false;
            }
            else
            {
                return user.Password.Equals(pw);
            }
        }


        public void AddSystemLogLogin(string UserId)
        {
         
            LogSystemVO logVO = new LogSystemVO();
            logVO.Action = MsgVO.LogTitleName.登入記錄.ToString();
            logVO.Fucntion = MsgVO.LogTitleName.登入記錄.ToString();
            logVO.SubFucntion = MsgVO.LogTitleName.登入記錄.ToString();
            logVO.IpAddress = httpHelper.GetUserIp(HttpContext.Current);
            logVO.UpdateId = UserId;
            logVO.UpdateDate = DateTime.Now;

            ISession session = NHibernateSessionManager.Instance.GetSession();
            session.Save(logVO);
        }

       

        /// <summary>
        /// 加入 system log
        /// </summary>
        /// <param name="action">異動行為</param>
        public void AddSystemLog(MsgVO.Action action, object obj)
        {

            SessionHelper sHelper = new SessionHelper();

            LoginUser userVO = sHelper.LoginUser;
            LogSystemVO logVO = sHelper.LogVO;

            if (!String.IsNullOrEmpty(logVO.Fucntion))
            {

                if (userVO != null)
                {
                    logVO.UpdateId = userVO.UserId;
                }

                logVO.UpdateDate = DateTime.Now;
                logVO.Action = action.ToString();
                logVO.UpdateClassName = obj.GetType().ToString();
                logVO.IpAddress = httpHelper.GetUserIp(HttpContext.Current);

                ISession session = NHibernateSessionManager.Instance.GetSession();
                session.Save(logVO);

            }
            else
            {
                // log.Debug("logVO.Function is null ,updateClassName "+obj.ToString());            
            }
        }

        /// <summary>
        /// 加入 system log
        /// </summary>
        /// <param name="action">異動行為</param>
        public void AddSystemLog(MsgVO.Action action, object obj, string function, string note)
        {

            SessionHelper sHelper = new SessionHelper();

            LoginUser userVO = sHelper.LoginUser;

            LogSystemVO logVO = new LogSystemVO();

            if (userVO != null)
            {
                logVO.UpdateId = userVO.UserId;
            }

            logVO.UpdateDate = DateTime.Now;
            logVO.Action = action.ToString();
            logVO.UpdateClassName = obj.GetType().ToString();
            logVO.Fucntion = function;
            logVO.SubFucntion = logVO.SubFucntion;
            logVO.Note = note;
            logVO.IpAddress = httpHelper.GetUserIp(HttpContext.Current);
            ISession session = NHibernateSessionManager.Instance.GetSession();
            session.Save(logVO);

        }
       
       
    }
}